﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace THO7AlgoritmTimerApplication {
    class find_pixel : VisionAlgorithm {
        public find_pixel(String name) : base(name) { }
        public override Bitmap DoAlgorithm(Bitmap sourceImage) {
            BitmapData bmpdata = sourceImage.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            IntPtr ptr = bmpdata.Scan0;
            int regellengte = Math.Abs(bmpdata.Stride);
            int bytes = regellengte * sourceImage.Height;
            byte[] rgbValues = new byte[bytes];
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
            int new_width = 0;
            int new_height = 0;
            int x1 = regellengte;
            int y1 = sourceImage.Height;
            int x2 = 0;
            int y2 = 0;
            for (int regelnummer = 0; regelnummer < sourceImage.Height - 1; regelnummer++) {
                for (int convolutie = 0; convolutie < regellengte; convolutie += 3) {
                    int pixel = regelnummer * regellengte + convolutie;
                    if (rgbValues[pixel] == 0 && rgbValues[pixel+1] == 0 && rgbValues[pixel+2] == 0) {
                        if (regelnummer < y1) {
                            y1 = regelnummer;
                        } if (regelnummer > y2) {
                            y2 = regelnummer;
                        } if (convolutie < x1) {
                            x1 = convolutie;
                        } if (convolutie > x2){
                            x2 = convolutie;
                        }
                    }
                }
            }
            new_width = x2 - x1;
            new_height = y2 - y1;
            int bytes1 = new_width * new_height;
            byte[] output = new byte[bytes1];
            int i = 0;
            int j = 0;
            for (int regelnummer = y1; regelnummer < y2; regelnummer++) {
                for (int convolutie = x1; convolutie < x2; convolutie += 3) {
                    int p = regelnummer * regellengte + convolutie;
                    int new_pixel = i * new_width + j;
                    output[new_pixel] = rgbValues[p];
                    output[new_pixel + 1] = rgbValues[p + 1];
                    output[new_pixel + 2] = rgbValues[p + 2];
                    j++;
                }
                i++;
            }
            Bitmap dest = new Bitmap(new_width, new_height);
            BitmapData bmpdata1 = dest.LockBits(new Rectangle(0, 0, new_width, new_height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
            IntPtr ptr1 = bmpdata1.Scan0;
            System.Runtime.InteropServices.Marshal.Copy(output, 0, ptr1, (new_height * regellengte));
            sourceImage.UnlockBits(bmpdata);
            dest.UnlockBits(bmpdata1);
            return dest;
        }
    }
}
